<template>
  <div>
    <el-card>
      <div class="header">
        <el-button type="primary" @click="handleAdd">添加角色</el-button>
      </div>
      <el-table
        :data="tableData"
        row-key="id"
        :tree-props="{children: 'children'}"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="角色名称">
        </el-table-column>
        <el-table-column
          label="角色部门">
          <template slot-scope="scope">
            <span v-if="scope.row.dname!=''&&scope.row.dname!=null">
              {{scope.row.dname}}
            </span>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.tenantId!=='0'" size="mini" type="primary" @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-popconfirm
              v-if="scope.row.tenantId!=='0'"
              title="确定要删除该角色吗？"
              style="margin-left: 5px"
              @onConfirm="handleDelete(scope.row.id)"
            >
              <el-button slot="reference" size="mini" type="danger">删除</el-button>
            </el-popconfirm>
            <span v-else>系统默认选项，无法操作</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <create ref="create" @Ok="handleInit"></create>
    <edit ref="edit" @Ok="handleInit"></edit>
  </div>
</template>

<script>
  import Create from './components/create'
  import Edit from './components/edit'
  import {deleteSalesRole, getRoleList} from "@/api/sales";

  export default {
    name: "index",
    components: {
      Create,
      Edit
    },
    data() {
      return {
        tableData: []
      }
    },
    mounted() {
      this.handleInit()
    },
    methods: {
      handleInit() {
        getRoleList().then(res => {
          this.tableData = res.result
        })
      },
      handleAdd() {
        this.$refs['create'].show()
      },
      handleEdit(data) {
        this.$refs['edit'].show(data)
      },
      handleDelete(id) {
        deleteSalesRole({id: id}).then(res => {
          if (res.status === 200) {
            this.handleInit();
            this.$message.success("删除成功")
          }
        })
      }
    }
  }
</script>

<style scoped>
  .header {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
</style>
